﻿using System;
using System.ComponentModel;
using System.Data;
using ReflectionStudio.Core.Events;
using System.Windows;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for EditEventWindow.xaml
    /// </summary>
    public partial class YARNA
    {
        #region ==EXCLUDEWH===========
        public static readonly DependencyProperty EXCLUDEWHProperty = DependencyProperty.Register("EXCLUDEWH",
            typeof(string), typeof(YARNA),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnEXCLUDEWHChanged));
        public string EXCLUDEWH
        {
            get
            {
                return (string)GetValue(EXCLUDEWHProperty);
            }
            set
            {
                SetValue(EXCLUDEWHProperty, value);
                if (MV != null)
                {
                    ((VGAAUF)MV).EXCLUDEWH = value;
                }
            }
        }
        private static void OnEXCLUDEWHChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as YARNA;
            if (src != null)
            {
                src.EXCLUDEWH = (string)e.NewValue;
            }
        }
        #endregion

        #region ==dwReq===========
        public static readonly DependencyProperty dwReqProperty = DependencyProperty.Register("dwReq",
        typeof(DataRow), typeof(YARNA),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwReqChanged));

        public DataRow dwReq
        {
            get
            {
                return (DataRow)GetValue(dwReqProperty);
            }
            set
            {
                SetValue(dwReqProperty, value);
                LOADBYREQ();
            }
        }
        private static void OdwReqChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as YARNA;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwReq = row;
            }
        }
        #endregion

        public YARNA()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VGAAUF();
            this.DataContext = MV;
            InitializeComponent();
            EXCLUDEWH = "01HMA";
        }
        public void LOADBYREQ()
        {
            if (!IsAuto)
                return;
            TBMain.Clear();
            if (dwReq == null)
                return;
            dwData = TBMain.Clone().NewRow();
            dwData["GAMANDANT"] = app.Mandant;
            dwData["GAART"] = dwReq["GAART"];
            dwData["GAVAR"] = dwReq["GAVAR"];
            dwData["GAFARBE"] = dwReq["GAFARBE"];
            if (LMMU == "N")
            {
                dwData["GAAUFMSL"] = "";
            }
            else
            {
                dwData["GAAUFMSL"] = " AND GLAUFMSL='" + dwReq["GAAUFMSL"].ToString() + "'";
            }
            dwData["GAFILLER"] = EXCLUDEWH;
            dwData["GABEM"] = dwReq["GABEM"].ToString().Trim();

            SResult = ((VGAAUF)MV).QGD120(dwData);
            MergeData(SResult, false, true);
            SResult = ((VGAAUF)MV).QGD1210(dwData);
            MergeData(SResult, false, false);

            SResult = ((VGAAUF)MV).QueryGAAUF(dwData, 13);

            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                string LO = row["LO"].ToString();
                string LB = row["LB"].ToString();
                string LP = row["LP"].ToString();
                string PLACE = (LO + LB + LP.Trim());
                foreach (DataRow ZU in TBMain.Rows)
                {
                    string ZULO = ZU["LO"].ToString();
                    string ZULB = ZU["LB"].ToString();
                    string ZULP = ZU["LP"].ToString();
                    string ZUPLACE = (ZULO + ZULB + ZULP.Trim());

                    if (ZULO + ZULB != LO + LB) continue;
                    if (PLACE.StartsWith(EXCLUDEWH)
                        && !ZUPLACE.StartsWith(EXCLUDEWH))
                    {
                        continue;
                    }
                    if (!PLACE.StartsWith(EXCLUDEWH)
                        && ZUPLACE.StartsWith(EXCLUDEWH)) continue;


                    if (ZU["GAART"].ToString() + ZU["GAVAR"].ToString() + ZU["GAFARBE"].ToString() + ZU["GPSPPART"].ToString() + ZU["GPFBPART"].ToString() + ZU["GLAUFMSL"].ToString()
                    != row["GAART"].ToString() + row["GAVAR"].ToString() + row["GAFARBE"].ToString() + row["GPSPPART"].ToString() + row["GPFBPART"].ToString() + row["GLAUFMSL"].ToString())
                        continue;

                    double NETKG = Convert.ToDouble(ZU["NETKG"].ToString());
                    double locked = Convert.ToDouble(ZU["QLOCK"].ToString());
                    double nLock = Convert.ToDouble(row["QLOCK"].ToString());
                    double QISSUE = Convert.ToDouble(row["QISSUE"].ToString());
                    ZU["QLOCK"] = locked + nLock - QISSUE;
                    ZU["QFREE"] = Math.Round(NETKG - (locked + nLock - QISSUE), 1);
                }
            }
            /*预扣数据暂时不作用
            SResult = ((VGAAUF)MV).QueryGAAUF(dwData, 14);

            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                string GABEM = row["GABEM"].ToString().Trim();
                if (GABEM == "")
                    continue;
                var query = from DataRow ZU in TBMain.AsEnumerable()
                            where ZU["GAART"].ToString() == row["GAART"].ToString()
                            && ZU["GAFARBE"].ToString() == row["GAFARBE"].ToString()
                            && ZU["GLAUFMSL"].ToString() == row["GAAUFMSL"].ToString()
                            select ZU;
                //找到任意一条对应的记录，就跳出循环。
                foreach (DataRow ZU in query)
                {
                    if (ZU["GPSPPART"].ToString().Trim() == GABEM
                        || ZU["GPFBPART"].ToString().Trim() == GABEM
                        )
                    {
                        double NETKG = Convert.ToDouble(ZU["NETKG"].ToString());
                        double locked = Convert.ToDouble(ZU["QLOCK"].ToString());
                        double QPLAN = Convert.ToDouble(ZU["QPLAN"].ToString());
                        double nLock = Convert.ToDouble(row["QPLAN"].ToString());
                        ZU["QPLAN"] = QPLAN + nLock;
                        ZU["QFREE"] = Math.Round(NETKG - (locked + nLock + QPLAN), 1);
                        break;
                    }
                }
            } */
            vwMaster.View.MoveCurrentToFirst();
        }
        public override void OnAutoChg(bool value)
        {
            if (!IsAuto)
                return;
            LOADBYREQ();
            base.OnAutoChg(value);
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);


        }
    }
}
